package com.csmz.mybookreadernew.Adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.RemoteViews;
import android.widget.TextView;

import com.csmz.mybookreadernew.DBSQLite.DbMySQLite;
import com.csmz.mybookreadernew.R;
import com.csmz.mybookreadernew.RssUtils.RssFeed;
import com.csmz.mybookreadernew.RssUtils.RssItem;

public class ListViewAdapter extends BaseAdapter {
    private RssFeed rssFeed;
    private Context context;
    public ListViewAdapter(){

    }
    public ListViewAdapter(Context context,RssFeed rssFeed){
        this.context=context;
        this.rssFeed=rssFeed;
    }

    @Override
    public int getCount() {
        return rssFeed.getItemCount();
    }

    @Override
    public Object getItem(int position) {
        return rssFeed.getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        if (convertView ==null){
            convertView=LayoutInflater.from(context).inflate(R.layout.list_item,parent,false);
            viewHolder=new ViewHolder();
            viewHolder.tv_title=convertView.findViewById(R.id.tv_title);
            viewHolder.tv_time=convertView.findViewById(R.id.tv_time);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        RssItem rssItem=rssFeed.getItem(position);
        viewHolder.tv_title.setText(rssItem.getTitle());
        viewHolder.tv_time.setText(rssItem.getPubdate());
//        saveDate(rssItem);
        return convertView;
    }
    private void saveDate(RssItem rssItem) {
        DbMySQLite dbMySQLite=new DbMySQLite(context);
        dbMySQLite.addNewInfo(rssItem.getTitle(),rssItem.getLink(),rssItem.getPubdate());
    }

    private class ViewHolder{
        TextView tv_title;
        TextView tv_time;
    }

}
